package cn.edu.nuc.ssm.util;

import java.util.List;

/**
 * @author zhangkairong 
 * @version 2017-3-20 下午9:24:05
 * 
 */
public class Pager<T> {

	/**
	 * 分页控制
	 */
	private PageControl pageControl;
	
	/**
	 * 需要显示的数据列表
	 */
	 //处理List的泛型

	private List<T> list;
	

	public PageControl getPageControl() {
		return this.pageControl;
	}

	public void setPageControl(PageControl pageControl) {
		this.pageControl = pageControl;
	}

	public List<T> getList() {
		return this.list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	@Override
	public String toString() {
		return "Pager [pageControl=" + pageControl + ", list=" + list + "]";
	}


	/**
	 * 处理分页参数
	 * @param pc
	 * @return
	 */
	public  PageControl detailPage(PageControl pc) {

		int sumPage = pc.getSumCount()/pc.getPagesize();
		if (pc.getSumCount()%pc.getPagesize()>0) {

			sumPage++;
		}
		pc.setSumPage(sumPage);

		int everyPageShowPageNum = pc.getEveryPageShowPageNum();//分页一次显示多少页
		int minpage = 0 ;//当前显示最大页码
		int maxpage = 0 ;
		int index = pc.getCurrentIndex();

		//当总数不足以达到一下页时
		if (sumPage <= everyPageShowPageNum) {

			maxpage = sumPage;
			minpage = 1;
		}else {

			int buff = everyPageShowPageNum/2;//取中间数。maxpage=index+buff
			buff = index + buff; //当前页加每页显示的页码
			if (buff <=everyPageShowPageNum) {  //可以有下一页，但在第一页且没有达到页码不间值
				maxpage = everyPageShowPageNum;
				minpage = 1;
			}else if (buff< sumPage) { // 中间页预算小于最大页码数时
				maxpage = buff;
				minpage = maxpage - everyPageShowPageNum + 1;
			}else if (buff >= sumPage) {
				maxpage = sumPage;
				minpage = maxpage - everyPageShowPageNum + 1;
			}
		}
		pc.setMaxPage(maxpage);
		pc.setMinPage(minpage);
		return pc;
	}
}
